from __future__ import division
from operator import itemgetter, attrgetter
from struct import *
# import gc
import copy
import math
import os
import random
import sys
import time
from sets import Set
from random import randint
import re
import numpy as np
from os import walk
# import matplotlib.pyplot as plt
from subprocess import call
import os

print "Begins..."
iters = []

def intsfromfile(f):
  while True:
     a = array.array('f')
     a.fromstring(f.read(4000))
     if not a:
         break
     for x in a:
         yield x

def recordsfromfile(f):
  while True:
     a = array.array('f')
     a.fromstring(f.read(4000))
     currentLine = f.readline()
     if not a:
         break
     for x in a:
         yield x

inputFileName1 = "/home/diaosi/outputDirForTermScores/outputTest.binary"
inputFileHandler1 = open(inputFileName1,"rb")

outputFileTestName = "test.txt"
outputFileTestHandler = open(outputFileTestName,"w")

iters.append(intsfromfile(inputFileHandler1))

a = array.array('f')
for x in heapq.merge(*iters):
  a.append(x)
  if len(a) >= 1000:
      # old version
      # a.tofile(outputFileHandler)
      # new version
      a = list(a)
      for element in a:
          outputFileTestHandler.write(str(element) + "\n");
      del a[:]
if a:
  # old version
  # a.tofile(outputFileHandler)
  # new version
  a = list(a)
  for element in a:
      outputFileTestHandler.write(str(element) + "\n");  

inputFileHandler1.close()
outputFileTestHandler.close()
print "done"
print "Ends."



